package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.model.domain.Log;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description 消息发送工具类
 * @date 2021/11/5
 */
@Service
public class MqMessageService {
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送日志消息
     * @param userId    用户id
     * @param type  日志操作类型
     * @param key   日志操作主体-user/movement/video
     * @param busId 操作主体对应的业务id：null(user) / movementId(movement)   /   videoId(video)
     */
    public void sendLogMessage(Long userId,String type,String key,String busId) {
        try {
            String logTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            Log log = Log.init(userId, logTime, type, busId);
            String message = JSON.toJSONString(log);
            amqpTemplate.convertAndSend("tanhua.log.exchange",
                    "log."+key, message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送动态审核消息
     * @param movementId    动态id
     */
    public void sendAudiMessage(String movementId) {
        try {
            amqpTemplate.convertAndSend("tanhua.audit.exchange",
                    "audit.movement",movementId);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }
}
